#ifndef PEFACTORY_H
#define PEFACTORY_H
#include "QtGui"
#include "qtpropertymanager.h"
#include "qtvariantproperty.h"
#include "qttreepropertybrowser.h"
#include "qtbuttonpropertybrowser.h"
#include "qtgroupboxpropertybrowser.h"
#include "qtcoco/CocoHieriachy.h"

#include "qtcoco/CocoHieriachy.h"
#include "qmap.h"


class PEditor 
{

public:
    QCocoNode m_qnode;
    QtProperty * m_topItem;
    QtVariantPropertyManager * m_variantManager;
	QMap<QString , QtVariantProperty * > m_map2Property;

    PEditor::PEditor(QtVariantPropertyManager *variantManager)
    {
        m_variantManager = variantManager;
		m_bIsDisplay = false;
    }
    void init();

	void initializeProperty(QtVariantProperty * property)
	{
		if (property->propertyType() == QtVariantPropertyManager::enumTypeId())
		{
			property->setValue(0);
			return;
		}
		switch(property->propertyType())
		{
		case QVariant::String :
			property->setValue("");
			break;
		case QVariant::Int :	
		case QVariant::Double :
			property->setValue(0);
			break;
		case QVariant::PointF:
			property->setValue(QPointF(0,0));
			break;
		case QVariant::SizeF:
			property->setValue(QSizeF(0,0));
			break;
		case QVariant::Color:
			property->setValue(QColor(0,0,0));
			break;
		case QVariant::Bool:
			property->setValue(0);
			break;
		}
		
	}

    QtProperty * TopItem(){return m_topItem;}
	QtVariantProperty * Property(QString name)
	{ 
		return m_map2Property.value(name);
	}
	
	QList<QString> getAllPropertyNames()
	{
		return m_map2Property.keys();
	}
	
    virtual QString name() = 0;
    virtual QtProperty * initUI() = 0;
    virtual void refresh() = 0;
	virtual void valueChanged(QtProperty* property, QVariant variant) = 0;
    virtual void refreshProperty(QString propertyName){}


	
protected:

    QtVariantProperty * PEditor::addProperty(int type , QString name)
    {
        QtVariantProperty *item = m_variantManager->addProperty(type ,name);
        //item->setValue(QPointF(m_node.m_node->getContentSize().width , m_node.m_node->getContentSize().height));

        m_topItem->addSubProperty(item);
		if (m_map2Property.contains(name))
		{
			m_map2Property.remove(name);
		}
		m_map2Property.insert(name, item);
		return item;
    }
	
public:
	bool m_bIsDisplay;
	void Update();
	void disable();
	void enable();
};




class PEFactory :public QObject
{
	Q_OBJECT
public:
    
    PEFactory();
    static PEFactory * shareFactory();
	QWidget * createPE(int tid);

    
    static bool m_bInit;
    
    void init();
    
    QMap<QString , PEditor * > m_mapControllers;

    
	QtVariantPropertyManager * m_variantManager;
	QtTreePropertyBrowser *m_variantEditor;
	QtVariantEditorFactory *m_variantFactory;
	void reg(QString str , PEditor  *controller)
    {
        m_mapControllers.insert(str, controller);
        
    }

    void unreg( QString key)
    {
        m_mapControllers.remove(key);
    }
	void removeAllQtProperty();
	PEditor * getPEditorByName(QString name);
public slots:

	void changeValue( QtProperty* property ,QVariant variant);
};

#endif // PEFACTORY_H
